﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using DimsMapper.Modules;

namespace DimsMapper
{
    class ScriptsSet
    {
        const int SCRIPT_TYPE_COUNT = 5;
        const int SCRIPT_LIST_EXTENT_SIZE = 16;

        class Script
        {
            int sid;
            int scr_next;
            int tile_time;
            int radius;
            int flags;
            int scr_script_idx;
            int scr_oid;
            int localVarsOffset;
            int localVarsCount;
            int value;
            int action;
            int fixedParam;
            int actionBeingUsed;
            int scriptOverrides;
            int field_48;
            int howMuch;
            int field_50;

            public Script(DBFile file)
            {
                sid = file.ReadInt();
                scr_next = file.ReadInt32();
                
                switch (sid >> 24)
                {
                    case 1: //  SCRIPT_TYPE_SPATIAL
                        tile_time = file.ReadInt32();
                        radius = file.ReadInt32();
                        break;
                    case 2: //SCRIPT_TYPE_TIMED
                        tile_time = file.ReadInt32();
                        break;
                }


                flags = file.ReadInt32();
                scr_script_idx = file.ReadInt32();
                file.SkipBytes(4);
                scr_oid = file.ReadInt32();
                localVarsOffset = file.ReadInt32();
                localVarsCount = file.ReadInt32();
                value = file.ReadInt32();
                action = file.ReadInt32();
                fixedParam = file.ReadInt32();
                actionBeingUsed = file.ReadInt32();
                scriptOverrides = file.ReadInt32();
                field_48 = file.ReadInt32();
                howMuch = file.ReadInt32();
                field_50 = file.ReadInt32();

                //if (!(map_data.flags & 1)) scr->localVarsCount = 0;
            }
        }

        class ScriptListExtent
        {
            public Script[] scripts = new Script[SCRIPT_LIST_EXTENT_SIZE];
            public int length; // Number of scripts in the extent
            //public ScriptListExtent next;
        }

        class ScriptList
        {
            public List<ScriptListExtent> extentList;
            //public ScriptListExtent head;
            //public ScriptListExtent tail;

            public int length; // Number of extents in the script list.
            //public int nextScriptId;
        }

        ScriptList[] scriptsList = new ScriptList[SCRIPT_TYPE_COUNT];


        void subLoad(DBFile file, ref ScriptListExtent extent)
        {
            for (int e = 0; e < SCRIPT_LIST_EXTENT_SIZE; e++)
            {
                extent.scripts[e] = new Script(file);
            }
            extent.length = file.ReadInt();
            int next = file.ReadInt32();
        }

        public ScriptsSet(DBFile file)
        {
            // Загрузка блока скриптов
            for (int i = 0; i < SCRIPT_TYPE_COUNT; i++)
            {
                int scriptsTotalCount = file.ReadInt();
                if (scriptsTotalCount != 0) {
                    scriptsList[i] = new ScriptList();

                    scriptsList[i].length = scriptsTotalCount / 16;
                    if ((scriptsTotalCount % 16) != 0) scriptsList[i].length++;

                    scriptsList[i].extentList = new List<ScriptListExtent>();

                    for (int n = 0; n < scriptsList[i].length; n++)
                    {
                        ScriptListExtent extent = new ScriptListExtent();
                        scriptsList[i].extentList.Add(extent);
                        subLoad(file, ref extent);
                    }
                }
            }
        }

    }
}
